package top.heue.utils.base.helper;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import top.heue.utils.base.helper.item.ItemHelper;


public class RecyclerAdapterHelper<T> extends RecyclerView.Adapter<RecyclerAdapterHelper.ViewHolder> {

    private List<T> list;
    private final int defaultLayout;
    private final int brId;
    private final ItemTouchHelper touch;
    private RecyclerView recyclerView;

    /**
     * 如果需要拖拽功能，记得在View中设置一个例如长按监听然后调用adapter的attachToRecyclerView()和startDrag();
     *
     * @param list
     * @param defaultLayout
     * @param brId
     */
    public RecyclerAdapterHelper(List<T> list, int defaultLayout, int brId) {
        this.list = list;
        this.defaultLayout = defaultLayout;
        this.brId = brId;
        touch = ItemHelper.INSTANCE.getItemTouchHelper(this);
    }

    public List<T> getAllItem() {
        return list;
    }

    public void setAllItem(List<T> list) {
        this.list = list;
    }

    public int getItemLayout(/*T list*/) {
        return defaultLayout;
    }

    @Override
    public int getItemViewType(int position) {
        return getItemLayout();
    }

    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), viewType, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.binding.setVariable(brId, list.get(position));
        holder.binding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ViewDataBinding binding;

        ViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    public void attachToRecyclerView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        touch.attachToRecyclerView(recyclerView);
    }

    public void startDrag(View view) {
        ViewHolder holder = (ViewHolder) recyclerView.findContainingViewHolder(view);
        assert holder != null;
        touch.startDrag(holder);
    }

    public void addItem(int position, T item) {
        list.add(position, item);
        notifyItemChanged(position);
    }

    public void addItem(T item) {
        list.add(item);
        notifyItemChanged(list.size() - 1);
    }

    public void changeItem(int position, T item) {
        list.set(position, item);
        notifyItemChanged(position);
    }

    public void changeItem(T item) {
        int pos = list.indexOf(item);
        list.set(pos, item);
        notifyItemChanged(pos);
    }

    @SuppressLint("NotifyDataSetChanged")
    public void removeAllItem() {
        list.clear();
        notifyDataSetChanged();
    }

    public void removeItem(int position) {
        list.remove(position);
        notifyItemRemoved(position);
    }

    public void removeItem(T item) {
        int pos = list.indexOf(item);
        list.remove(pos);
        notifyItemRemoved(pos);
    }

    @SuppressLint("NotifyDataSetChanged")
    public void changeAllItem(List<T> itemData) {
        this.list = itemData;
        notifyDataSetChanged();
    }

    public void changeRangeItem(List<T> itemData, int positionStart, int itemCount) {
        this.list = itemData;
        notifyItemRangeChanged(positionStart, itemCount);
    }

    public void insertRangeItem(List<T> itemData, int positionStart, int itemCount) {
        this.list = itemData;
        notifyItemRangeInserted(positionStart, itemCount);
    }

    public void removeRangeItem(List<T> itemData, int positionStart, int itemCount) {
        this.list = itemData;
        notifyItemRangeRemoved(positionStart, itemCount);
    }

}